package com.ooqiu.gaming.ui.main.thread.common;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.ooqiu.gaming.db.Channel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eenie
 * @date 2017/11/17
 */

public abstract class ChannelPageAdapter extends FragmentPagerAdapter {
    List<Channel> mChannels = new ArrayList<>();
    FragmentManager mFragmentManager;
    Channel mChannel = new Channel(null, "0", "0", "推荐", Channel.MODULE_ID_INDEX, true, "");

    public ChannelPageAdapter(FragmentManager fm, List<Channel> channels) {
        super(fm);
        mFragmentManager = fm;
        mChannels.add(mChannel);
        mChannels.addAll(channels);
    }
    public ChannelPageAdapter(FragmentManager fm, List<Channel> channels,boolean addDefault) {
        super(fm);
        mFragmentManager = fm;
        mChannels.addAll(channels);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = mFragmentManager.findFragmentByTag(mChannels.get(position).getCat_id());
        if (fragment == null) {
            fragment = createFragment(mChannels.get(position));
        }
        return fragment;
    }

    public abstract Fragment createFragment(Channel channel);

    @Override
    public int getCount() {
        return mChannels.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return mChannels.get(position).getTypename();
    }
}
